package com.proactive.webapp.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.proactive.webapp.dao.CourseDAO;
import com.proactive.webapp.dao.FollowDAO;
import com.proactive.webapp.dao.MemberDAO;
import com.proactive.webapp.dao.MenuDAO;
import com.proactive.webapp.entity.Course;
import com.proactive.webapp.entity.Follow;
import com.proactive.webapp.entity.Member;
import com.proactive.webapp.entity.Menu;
import com.proactive.webapp.util.MenuUtil;
import com.proactive.webapp.util.PageCount;

@Controller
public class FollowController {
	ApplicationContext context =  new ClassPathXmlApplicationContext("spring-module.xml");
	
	@RequestMapping(value = "/addFollow", method = RequestMethod.POST)
	public @ResponseBody void addFollow(String id, HttpSession session){
		Member loginMember = (Member)session.getAttribute("loginMember");
		if(loginMember != null){
			String loginAccount = loginMember.getAccount();
			FollowDAO followDAO = (FollowDAO) context.getBean("followDAO");
			MemberDAO memberDAO = (MemberDAO) context.getBean("memberDAO");
			Follow follow = new Follow();
			Member member = new Member();
			member.setMemberNO(id);
			member = memberDAO.getByMemberNO(member);
			follow.setAccount(loginAccount);
			follow.setFollowAccount(member.getAccount());
			followDAO.insert(follow);
		}
	}
	
	@RequestMapping(value = "/delFollow", method = RequestMethod.POST)
	public @ResponseBody void delFollow(String id, HttpSession session){
		Member loginMember = (Member)session.getAttribute("loginMember");
		if(loginMember != null){
			String loginAccount = loginMember.getAccount();
			FollowDAO followDAO = (FollowDAO) context.getBean("followDAO");
			MemberDAO memberDAO = (MemberDAO) context.getBean("memberDAO");
			Follow follow = new Follow();
			Member member = new Member();
			member.setMemberNO(id);
			member = memberDAO.getByMemberNO(member);
			follow.setAccount(loginAccount);
			follow.setFollowAccount(member.getAccount());
			followDAO.delete(follow);
		}
	}
	
	@RequestMapping(value = "/memberFollow", method = RequestMethod.GET)
	public ModelAndView getFollowList(HttpServletRequest request, HttpSession session) {
		ModelAndView model = new ModelAndView();
		Member loginMember = (Member)session.getAttribute("loginMember");
		
		if (request.getParameter("id") != null) {
			MemberDAO memberDAO = (MemberDAO) context.getBean("memberDAO");
			CourseDAO courseDAO = (CourseDAO) context.getBean("courseDAO");
			List<Member> memberList = new ArrayList<Member>();
			Member member = new Member();			
			//List<Course> courseList = new ArrayList<Course>();
			
			String memberNO = request.getParameter("id");
			int pageNow = request.getParameter("p")==null?1:Integer.parseInt(request.getParameter("p"));
			
			member.setMemberNO(memberNO);
			member = memberDAO.getByMemberNO(member);//用memberNO取得memberAccount
			String loginAccount = loginMember==null?"":loginMember.getAccount();
			member = memberDAO.getByLogin(member, loginAccount);//取得member資料
			int count = memberDAO.countFollowTotal(member.getAccount());
			
			//set pager
			int pageSize = 6;
			String pageUrl = "memberFollow";
			String pageEndUrl1 = "&id="+memberNO;
			PageCount pageCount = new PageCount(pageNow, pageSize, count, pageUrl, pageEndUrl1);
			pageCount.calculate();
			
			memberList = memberDAO.getFollowList(member.getAccount(), loginAccount, pageCount.getStart(), pageSize);
			for (int i = 0; i < memberList.size(); i++) {
				List<Course> courseList = new ArrayList<Course>();
				courseList = courseDAO.getListByMemberTop(memberList.get(i).getAccount(), 3);
				memberList.get(i).setCourseList(courseList);
			}
			
			List<Menu> menuList = MenuUtil.getList(((MenuDAO)context.getBean("menuDAO")).getList(), 0);
			
			
			//set model
			model.addObject("page", pageCount);
			model.addObject("member", member);
			model.addObject("memberList", memberList);
			model.addObject("menuList", menuList);
			model.setViewName(loginMember!=null?"memberFollow1":"memberFollow2");
			
		} 
		else
			model.setViewName(loginMember!=null?"redirect:/memberFollow?id="+loginMember.getMemberNO():"redirect:/login");

		return model;
	}
	
	@RequestMapping(value = "/memberFans", method = RequestMethod.GET)
	public ModelAndView getFansList(HttpServletRequest request, HttpSession session) {
		ModelAndView model = new ModelAndView();
		Member loginMember = (Member)session.getAttribute("loginMember");
		
		if (request.getParameter("id") != null) {
			MemberDAO memberDAO = (MemberDAO) context.getBean("memberDAO");
			CourseDAO courseDAO = (CourseDAO) context.getBean("courseDAO");
			List<Member> memberList = new ArrayList<Member>();
			Member member = new Member();			
			//List<Course> courseList = new ArrayList<Course>();
			
			String memberNO = request.getParameter("id");
			int pageNow = request.getParameter("p")==null?1:Integer.parseInt(request.getParameter("p"));
			
			member.setMemberNO(memberNO);
			member = memberDAO.getByMemberNO(member);//用memberNO取得memberAccount
			String loginAccount = loginMember==null?"":loginMember.getAccount();
			member = memberDAO.getByLogin(member, loginAccount);//取得member資料
			int count = memberDAO.countFansTotal(member.getAccount());
			
			//set pager
			int pageSize = 6;
			String pageUrl = "memberFans";
			String pageEndUrl1 = "&id="+memberNO;
			PageCount pageCount = new PageCount(pageNow, pageSize, count, pageUrl, pageEndUrl1);
			pageCount.calculate();
			
			memberList = memberDAO.getFansList(member.getAccount(), loginAccount, pageCount.getStart(), pageSize);
			for (int i = 0; i < memberList.size(); i++) {
				List<Course> courseList = new ArrayList<Course>();
				courseList = courseDAO.getListByMemberTop(memberList.get(i).getAccount(), 3);
				memberList.get(i).setCourseList(courseList);
			}
			
			List<Menu> menuList = MenuUtil.getList(((MenuDAO)context.getBean("menuDAO")).getList(), 0);
			
			
			//set model
			model.addObject("page", pageCount);
			model.addObject("member", member);
			model.addObject("memberList", memberList);
			model.addObject("menuList", menuList);
			model.setViewName(loginMember!=null?"memberFans1":"memberFans2");
			
		} 
		else
			model.setViewName(loginMember!=null?"redirect:/memberFans?id="+loginMember.getMemberNO():"redirect:/login");

		return model;
	}
}
